<template>
  <ContentWrap v-loading="infoLoading">
    <el-descriptions :title="t('judgeScene.card.title')" :column="5" direction="vertical" border>
      <el-descriptions-item :label="t('judgeScene.card.awardsName')">
        {{ awardsInfo.awardsName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.awards')">
        {{ awardsInfo.templateName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.stage')">
        {{ getJudgeStatus }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.currentRound')">
        {{ getCurrentRoundName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('judgeScene.card.studentNum')">
        <p>
          {{ t('instance.fields.total') }}:
          {{ (awardsInfo.masterCount ?? 0) + (awardsInfo.doctorCount ?? 0) }}
        </p>
        <p>{{ t('instance.fields.masterTotal') }}: {{ awardsInfo.masterCount }}</p>
        <p>{{ t('instance.fields.doctorTotal') }}: {{ awardsInfo.doctorCount }}</p>
      </el-descriptions-item>
      <template #extra>
        <el-button type="primary" plain @click="handleFinalList">
          {{ t('judgeScene.final.finalList') }}
        </el-button>
        <el-button type="primary" plain @click="handleFinalSettings">
          {{ t('judgeScene.final.finalSettings') }}
        </el-button>
        <el-button type="primary" plain @click="handleFinalCandidateSettings">
          {{ t('judgeScene.final.candidateSettings') }}
        </el-button>
        <el-button type="primary" plain @click="handleDebateStudent">
          {{ t('judgeScene.final.debateStudent') }}
        </el-button>
      </template>
    </el-descriptions>
  </ContentWrap>

  <!-- 最终名单 -->
  <ScxFinalWinningRecord ref="scxFinalWinningRecordRef" />
  <!-- 终评设置 -->
  <ScxFinalSettingsForm ref="scxFinalSettingsFormRef" @success="getInfo" />
  <!-- 答辩设置 -->
  <ScxFinalDebateTimeForm ref="scxFinalDebateTimeFormRef" />
  <!-- 答辩学生安排 -->
  <ScxDebateStudentList ref="scxDebateStudentListRef" />
</template>

<script setup lang="ts">
import ScxFinalDebateTimeForm from '@/views/awards/scxfinaldebatetime/ScxFinalDebateTimeForm.vue'
import ScxFinalSettingsForm from '@/views/awards/scxfinalsettings/ScxFinalSettingsForm.vue'
import { InstanceApi, InstanceVO } from '@/api/awards/instance'
import ScxDebateStudentList from '@/views/awards/scxfinalcandidate/components/ScxDebateStudentList.vue'
import { ReviewSceneApi, StageRoundVO } from '@/api/awards/reviewscene'
import ScxFinalWinningRecord from '@/views/awards/scxfinalwinningrecord/index.vue'

const route = useRoute()
const router = useRouter() // 路由对象
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const getJudgeStatus = computed(() => {
  // 1=未开始 2=初评进行中 3=初评结束 4=终评进行中 5=终评结束
  switch (awardsInfo.value.judgeStatus) {
    case 1:
      return t('judgeScene.card.notStart')
    case 2:
      return t('judgeScene.card.initialUnderway')
    case 3:
      return t('judgeScene.card.initialEnd')
    case 4:
      return t('judgeScene.card.finalUnderway')
    case 5:
      return t('judgeScene.card.finalEnd')
    default:
      return t('judgeScene.card.notStart')
  }
})

const finalStage = computed(() => {
  return stageRoundInfos.value?.[1]
})

const getCurrentRoundName = computed(() => {
  return (finalStage.value?.round ?? []).find((item) => item.currentRound)?.name ?? 'N/A'
})

const infoLoading = ref(false)
const awardsInfo = ref<InstanceVO>({} as InstanceVO)
const stageRoundInfos = ref<StageRoundVO[]>()
const getInfo = async () => {
  infoLoading.value = true
  const [awardsInfoRes, stageRoundInfosRes] = await Promise.all([
    // 读取评奖基本信息
    InstanceApi.getInstance(awardsId.value),
    // 读取阶段和轮次信息信息
    ReviewSceneApi.getStageRound(awardsId.value)
  ])
  awardsInfo.value = awardsInfoRes
  stageRoundInfos.value = stageRoundInfosRes
  infoLoading.value = false
}

// 最终名单
const scxFinalWinningRecordRef = ref<InstanceType<typeof ScxFinalWinningRecord>>()
const handleFinalList = () => {
  scxFinalWinningRecordRef.value?.open(awardsId.value)
}

// 终评设置
const scxFinalSettingsFormRef = ref<InstanceType<typeof ScxFinalSettingsForm>>()
const handleFinalSettings = () => {
  scxFinalSettingsFormRef.value?.open(awardsId.value)
}

// 答辩设置
const scxFinalDebateTimeFormRef = ref<InstanceType<typeof ScxFinalDebateTimeForm>>()
const handleFinalCandidateSettings = () => {
  scxFinalDebateTimeFormRef.value?.open(awardsId.value)
}

// 答辩学生安排
const scxDebateStudentListRef = ref<InstanceType<typeof ScxDebateStudentList>>()
const handleDebateStudent = () => {
  scxDebateStudentListRef.value?.open(awardsId.value)
}

const awardsId = ref('')
/** 初始化 **/
onMounted(() => {
  if (!route.params.instanceId) {
    router.push('/awards/instance')
    return
  }
  awardsId.value = route.params.instanceId as string
  // 查询评审进行信息
  getInfo()
})
</script>
